package com.tanhua.dubbo.api;


import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class CommentApiImpl implements CommentApi {


    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public Integer save(Comment comment) {
        //查询动态,获取动态Movement对象
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
        //判断动态是否为空
        if (movement != null) {
            comment.setPublishUserId(movement.getUserId());
        }
        //保存动态数据
        mongoTemplate.save(comment);
        //更新动态表
        Query query = new Query(Criteria.where("id").is(comment.getPublishId()));
        //创建更新对象
        Update update = new Update();
        //判断类型是否是点赞
        if (comment.getCommentType() == CommentType.LIKE.getType()) {
            //判断类型是否是点赞，让攒点加1
            update.inc("likeCount", 1);
        } else if (comment.getCommentType() == CommentType.COMMENT.getType()) {
            //判断类型是否是评论
            update.inc("commentCount", 1);
        } else {
            //判断类型是否是喜欢
            update.inc("loveCount", 1);
        }
        //获取更新后的数据
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Movement modify = mongoTemplate.findAndModify(query, update, options, Movement.class);
        //获取最新动态数量,通过调方法根据类型统计数量
        return modify.statisCount(comment.getCommentType());
    }

    /**
     * 查询评论
     *
     * @param movementId
     * @param commentType
     * @param page
     * @param pageseze
     * @return
     */
    @Override
    public List<Comment> findComments(String movementId, CommentType commentType, Integer page, Integer pageseze) {
        Query query = Query.query(Criteria.where("publishId").is(new ObjectId(movementId)).and("commentType")
                .is(commentType.getType()))
                .skip((page - 1) * pageseze)
                .limit(pageseze).with(Sort.by(Sort.Order.desc("created")));
        return mongoTemplate.find(query, Comment.class);
    }

    /**
     * 动态点赞
     *
     * @param movementId
     * @param userId
     * @param commentType
     * @return
     */
    @Override
    public Boolean hasComment(String movementId, Long userId, CommentType commentType) {
        Criteria criteria = Criteria.where("userId").is(userId)
                .and("publishId").is(new ObjectId(movementId))
                .and("commentType").is(commentType.getType());
        Query query = new Query(criteria);
        return mongoTemplate.exists(query, Comment.class);
    }

    @Override
    public Integer delete(Comment comment) {
        //查询动态,获取动态Movement对象
        Criteria criteria = Criteria.where("userId").is(comment.getUserId())
                .and("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType());

        Query query = Query.query(criteria);
        //删除数据
        mongoTemplate.remove(query, Comment.class);
        //更新动态表
        Query query1 = new Query(Criteria.where("id").is(comment.getPublishId()));
        //创建更新对象
        Update update = new Update();
        //判断类型是否是点赞
        if (comment.getCommentType() == CommentType.LIKE.getType()) {
            //判断类型是否是点赞，让攒点加1
            update.inc("likeCount", -1);
        } else if (comment.getCommentType() == CommentType.COMMENT.getType()) {
            //判断类型是否是评论
            update.inc("commentCount", -1);
        } else {
            //判断类型是否是喜欢
            update.inc("loveCount", -1);
        }
        //获取更新后的数据
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Movement modify = mongoTemplate.findAndModify(query1, update, options, Movement.class);
        //获取最新动态数量,通过调方法根据类型统计数量
        return modify.statisCount(comment.getCommentType());
    }

    /**
     * 评论点赞
     */
    @Override
    public Integer pingLunLike(String movementId) {
        Query query = new Query(Criteria.where("id").is(new ObjectId(movementId)));
        Update update = new Update();
        update.inc("likeCount", 1);
        //获取更新后的数据
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Comment comment = mongoTemplate.findAndModify(query, update, options, Comment.class);
        return comment.getLikeCount();
    }

    /**
     * 取消评论点赞
     *
     * @param movementId
     * @return
     */
    @Override
    public Integer disLike(String movementId) {
        Query query = new Query(Criteria.where("id").is(new ObjectId(movementId)));
        Update update = new Update();
        update.inc("likeCount", -1);
        //获取更新后的数据
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Comment comment = mongoTemplate.findAndModify(query, update, options, Comment.class);
        return comment.getLikeCount();
    }

    /**
     * 查询点赞/评论/喜欢列表的公共方法
     * 条件：枚举类型
     *
     * @param userId
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public List<Comment> findLikeCommentsLove(Long userId, Integer page, Integer pagesize,CommentType commentType) {
        Query query = Query.query(Criteria.where("commentType").is(commentType.getType()).and("publishUserId").is(userId))
                .skip((page - 1) * pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        return mongoTemplate.find(query, Comment.class);
    }


    /**
     * 根据动态id查询所有的评论
     * @param page
     * @param pagesize
     * @param messageId
     * @return
     */
    @Override
    public List<Comment> findAllComments(Integer page, Integer pagesize, String messageId) {
        Criteria criteria = Criteria.where("publishId").is(new ObjectId(messageId)).and("commentType").is(CommentType.COMMENT.getType());
        Query query = Query.query(criteria).skip((page-1)*pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("created")));
        //通过条件查找动态的所有评论
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        return comments;
    }
}
